<?php
/*
	[Bidcms.com!] (C)2009-2011 Bidcms.com.
	This is NOT a freeware, use is subject to license terms
	$author limengqi
	$Id: userclass.php 2016-03-24 10:42 $
*/
if(!defined('IN_BIDCMS')) {
	exit('Access Denied');
}
class yun
{
    private $config = array();
    public $success = 'success';
    public $error = 'failed';
    public function __construct($data = array()){
      $this->config = $data;
    }
    public function setConfig($config){
      $this->config = array_merge($this->config,$config);
    }
    public function submit(){
        if($this->config['payment'] == 'balance'){
          return $this->balance();
        } elseif($this->config['payment'] == 'alipay'){
          return $this->alipay();
        } elseif($this->config['payment'] == 'wxpay'){
          return $this->wxpay();
        }
    }
    public function getRule($data){
      return array(
        'order_no' => $data['order_no'],
        'trade_no' => $data['trade_no'],
        'amount' => $data['amount'],
        'point' => $data['point'],
        'result' => $data
      );
    }
    public function balance(){
      $post = array(
          'order_no' => $this->config['order_no'], //订单号
          'amount' => $this->config['total_fee'], //金额
          'point' => $this->config['point'], //积分
          'customer_id' => $this->config['customer_id'],
          'notify_url' => $this->config['notify_url'],
          'shop_id' => $this->config['shop_id'],
          'merchant_id' => $this->config['merchant_id']
      );
      $post['sign'] = $this->getSign($post);
      $post['content'] = $this->config['content'];
      $data=bidcms_encode($post);
      $content=$GLOBALS['curl']->request($this->config['gateway'].'?con=pay&act='.$this->config['payment'],'PUT',$data); 
      if(is_null(json_decode($content))){
        $content = '{"errcode":-1,"errmsg":"'.$content.'"}';
      }
      return $content;
    }
  
    public function alipay(){
      $post = array(
          'order_no' => $this->config['order_no'], //订单号
          'amount' => $this->config['total_fee'], //金额
          'point' => $this->config['point'], //积分
          'customer_id' => $this->config['customer_id'],
          'notify_url' => $this->config['notify_url'],
          'shop_id' => $this->config['shop_id'],
          'merchant_id' => $this->config['merchant_id']
      );
      $post['sign'] = $this->getSign($post);
      $post['type'] = 'JSAPI';
      $post['content'] = $this->config['content'];
      $data=bidcms_encode($post);
      $content=$GLOBALS['curl']->request($this->config['gateway'].'?con=pay&act='.$this->config['payment'],'PUT',$data); 
      if(is_null(json_decode($content))){
        $content = '{"errcode":-1,"errmsg":"'.$content.'"}';
      }
      return $content;
    }
  
    public function wxpay(){
      $post = array(
          'order_no' => $this->config['order_no'], //订单号
          'amount' => $this->config['total_fee'], //金额
          'point' => $this->config['point'], //积分
          'customer_id' => $this->config['customer_id'],
          'notify_url' => $this->config['notify_url'],
          'shop_id' => $this->config['shop_id'],
          'merchant_id' => $this->config['merchant_id']
      );
      $post['sign'] = $this->getSign($post);
      $post['type'] = 'JSAPI';
      $post['content'] = $this->config['content'];
      $post['app_id'] = $this->config['app_id'];
      $post['open_id'] = $this->config['open_id'];
      $data=bidcms_encode($post);
      $content=$GLOBALS['curl']->request($this->config['gateway'].'?con=pay&act=wxpay','PUT',$data); 
      if(is_null(json_decode($content))){
        $content = '{"errcode":-1,"errmsg":"'.$content.'"}';
      }
      return $content;
    }
    /**
     * 请求加密
     *
     * @param array $params
     */
    private function getSign($params, $sort = true)
    {
        $sort && ksort($params);
        $params['key'] = $this->config['merchant_key'];

        $signStr = $this->buildQuery($params);
        return strtoupper(md5($signStr));
    }
    /**
     * build
     *
     * @param array $params
     */
    private function buildQuery($params)
    {
        $array = array();
        foreach ($params as $k => $v) {
            if ($v != '' && $k != 'sign') {
                $array[] = "$k=$v";
            }
        }
        $queryStr = implode('&', $array);
        return $queryStr;
    }
    /**
     * 验证签名信息
     *
     * @see \Common\Lib\Pay\Driver::verify()
     */
    public function verify($params)
    {
        if ($params['errcode'] != '1') {
            return false;
        }
        $remoteSign = $params['sign'];
        $post = array(
          'order_no' => $params['order_no'], //订单号
          'trade_no' => $params['trade_no'], //订单号
          'amount' => $params['amount'], //金额
          'point' => $params['point'], //积分
          'status' => $params['status']
        );
        $localSign = $this->getSign($post);

        if ($remoteSign != $localSign) {
            return false;
        }
        return true;
    }

}
